package com.bl.weilan.service.biz.tradedetail.impl;

import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.TradeChartDetailEntity;
import com.bl.weilan.dao.model.TradeChartEntity;
import com.bl.weilan.dao.repository.TradeChartDetailRepository;
import com.bl.weilan.dao.repository.TradeChartRepository;
import com.bl.weilan.service.biz.trade.TradeService;
import com.bl.weilan.service.biz.tradedetail.TradeDetailService;
import com.bl.weilan.service.vo.tradec.TradeVo;
import com.bl.weilan.service.vo.tradedetail.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class TradeDetailServiceImpl implements TradeDetailService {

    @Autowired
    TradeService tradeService;

    @Autowired
    TradeChartDetailRepository tradeChartDetailRepository;

    @Autowired
    TradeChartRepository tradeChartRepository;

    String[] rangeArray = {"2010","2011","2012","2013","2014","2015","2016","2017","2018","2019"};

    @Override
    public TradeDetailVo getTradeBarDetailVo(String tradeId) {

        TradeDetailVo tradeDetailVo = new TradeDetailVo();

        List<TradeChartEntity> tradeChartEntityList = tradeChartRepository.findByTradeId(tradeId);
        for (TradeChartEntity tradeChartEntity : tradeChartEntityList) {
            // 投资金额，总估值
            if ("投资事件".equals(tradeChartEntity.getName())) {
                TradeBarThingVo tradeBarThingVo = new TradeBarThingVo();
                tradeBarThingVo.setName(tradeChartEntity.getName());
                tradeBarThingVo.setType(tradeChartEntity.getType());
                tradeBarThingVo.setUnit(tradeChartEntity.getUnit());
                tradeBarThingVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradeBarThingVo.setUpdateTime(tradeChartEntity.getUpdateTime());
                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByYear(tradeChartEntity.getId());

                List<String> range = new ArrayList<>();
                List<String> data = new ArrayList<>();

                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {
                    range.add(tradeChartDetailEntity.getYear() + "");
                    data.add(tradeChartDetailEntity.getValue() + "");
                }
                int k = 0;
                String[] dataArray = {"0","0","0","0","0","0","0","0","0","0"};
                for (String rangeItem : rangeArray) {
                    int i = 0;
                    for (String item : range) {
                        if (item.equals(rangeItem)) {
                            dataArray[k] = data.get(i);
                        }
                        i ++;
                    }
                    k ++;
                }

                tradeBarThingVo.setRange(rangeArray);
                tradeBarThingVo.setData(dataArray);

                tradeDetailVo.setTradeBarThingVo(tradeBarThingVo);
            } else if ("投资轮次".equals(tradeChartEntity.getName())) {
                TradeBarsRoundVo tradeBarsRoundVo = new TradeBarsRoundVo();
                tradeBarsRoundVo.setTitle(tradeChartEntity.getName());
                tradeBarsRoundVo.setType(tradeChartEntity.getType());
                tradeBarsRoundVo.setUnit(tradeChartEntity.getUnit());
                tradeBarsRoundVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradeBarsRoundVo.setUpdateTime(tradeChartEntity.getUpdateTime());


                List<String> range = new ArrayList<>();

                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByYear(tradeChartEntity.getId());
                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {

                    if (!range.contains(tradeChartDetailEntity.getYear() + "")) {
                        range.add(tradeChartDetailEntity.getYear() + "");
                    }
                }

                // 记录融资轮次
                List<String> rounds = new ArrayList<>();

                List<TradeChartDetailEntity> tradeChartDetailEntityListB = tradeChartDetailRepository.findByChartIdOrderByYear(tradeChartEntity.getId());
                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityListB) {
                    if (!rounds.contains(tradeChartDetailEntity.getName() + "")) {
                        rounds.add(tradeChartDetailEntity.getName() + "");
                    }
                }

                List<TradeBarsVo> tradeBarsVos = new ArrayList<>();
                for (String round : rounds) {
                    TradeBarsVo tradeBarsVo = new TradeBarsVo();
                    tradeBarsVo.setName(round);
                    List<String> data = new ArrayList<>();
                    for (String year : range) {
                        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndNameAndYear(tradeChartEntity.getId(), round, Integer.parseInt(year));
                        if (tradeChartDetailEntity != null) {
                            data.add(tradeChartDetailEntity.getValue() + "");
                        } else {
                            data.add(0 + "");
                        }
                    }
                    String[] dataArray = new String[data.size()];
                    data.toArray(dataArray);
                    tradeBarsVo.setData(dataArray);
                    tradeBarsVos.add(tradeBarsVo);
                }


                String[] rangeArray = new String[range.size()];
                range.toArray(rangeArray);
                tradeBarsRoundVo.setRange(rangeArray);
                tradeBarsRoundVo.setData(tradeBarsVos);
                tradeDetailVo.setTradeBarsRoundVo(tradeBarsRoundVo);

            } else if ("成立时间".equals(tradeChartEntity.getName())) {
                TradePieFoundTimeVo tradePieFoundTimeVo = new TradePieFoundTimeVo();
                tradePieFoundTimeVo.setTitle(tradeChartEntity.getName());
                tradePieFoundTimeVo.setType(tradeChartEntity.getType());
                tradePieFoundTimeVo.setUnit(tradeChartEntity.getUnit());
                tradePieFoundTimeVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradePieFoundTimeVo.setUpdateTime(tradeChartEntity.getUpdateTime());

                List<TradePieVo> tradePieVoList = new ArrayList<>();
                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByValue(tradeChartEntity.getId());
                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {
                    TradePieVo tradePieVo = new TradePieVo();
                    tradePieVo.setName(tradeChartDetailEntity.getName() + "");
                    tradePieVo.setValue(tradeChartDetailEntity.getValue() + "");
                    tradePieVoList.add(tradePieVo);
                }
                tradePieFoundTimeVo.setTradePieVoList(tradePieVoList);

                tradeDetailVo.setTradePieFoundTimeVo(tradePieFoundTimeVo);

            } else if ("成立地点".equals(tradeChartEntity.getName())) {
                TradePieLocationVo tradePieLocationVo = new TradePieLocationVo();
                tradePieLocationVo.setTitle(tradeChartEntity.getName());
                tradePieLocationVo.setType(tradeChartEntity.getType());
                tradePieLocationVo.setUnit(tradeChartEntity.getUnit());
                tradePieLocationVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradePieLocationVo.setUpdateTime(tradeChartEntity.getUpdateTime());

                List<TradePieVo> tradePieVoList = new ArrayList<>();
                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByValue(tradeChartEntity.getId());
                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {
                    TradePieVo tradePieVo = new TradePieVo();
                    tradePieVo.setName(tradeChartDetailEntity.getName() + "");
                    tradePieVo.setValue(tradeChartDetailEntity.getValue() + "");
                    tradePieVoList.add(tradePieVo);
                }
                tradePieLocationVo.setTradePieVoList(tradePieVoList);

                tradeDetailVo.setTradePieLocationVo(tradePieLocationVo);

            } else if ("总估值".equals(tradeChartEntity.getName())) {
                TradeBarValuationVo tradeBarValuationVo = new TradeBarValuationVo();
                tradeBarValuationVo.setName(tradeChartEntity.getName());
                tradeBarValuationVo.setType("polyline");
                tradeBarValuationVo.setUnit(tradeChartEntity.getUnit());
                tradeBarValuationVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradeBarValuationVo.setUpdateTime(tradeChartEntity.getUpdateTime());
                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByYear(tradeChartEntity.getId());

                List<String> range = new ArrayList<>();
                List<String> data = new ArrayList<>();

                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {
                    range.add(tradeChartDetailEntity.getYear() + "");
                    data.add(tradeChartDetailEntity.getValue() + "");
                }
                int k = 0;
                String[] dataArray = {"0","0","0","0","0","0","0","0","0","0"};
                for (String rangeItem : rangeArray) {
                    int i = 0;
                    for (String item : range) {
                        if (item.equals(rangeItem)) {
                            dataArray[k] = data.get(i);
                        }
                        i ++;
                    }
                    k ++;
                }

                tradeBarValuationVo.setRange(rangeArray);
                tradeBarValuationVo.setData(dataArray);

                tradeDetailVo.setTradeBarValuationVo(tradeBarValuationVo);

            } else if ("投资金额".equals(tradeChartEntity.getName())) {
                TradeBarMoneyVo tradeBarMoneyVo = new TradeBarMoneyVo();
                tradeBarMoneyVo.setName(tradeChartEntity.getName());
                tradeBarMoneyVo.setType(tradeChartEntity.getType());
                tradeBarMoneyVo.setUnit(tradeChartEntity.getUnit());
                tradeBarMoneyVo.setCreateTime(tradeChartEntity.getCreateTime());
                tradeBarMoneyVo.setUpdateTime(tradeChartEntity.getUpdateTime());
                List<TradeChartDetailEntity> tradeChartDetailEntityList = tradeChartDetailRepository.findByChartIdOrderByYear(tradeChartEntity.getId());

                List<String> range = new ArrayList<>();
                List<String> data = new ArrayList<>();

                for (TradeChartDetailEntity tradeChartDetailEntity :tradeChartDetailEntityList) {
                    range.add(tradeChartDetailEntity.getYear() + "");
                    data.add(tradeChartDetailEntity.getValue() + "");
                }

                int k = 0;
                String[] dataArray = {"0","0","0","0","0","0","0","0","0","0"};
                for (String rangeItem : rangeArray) {
                    int i = 0;
                    for (String item : range) {
                        if (item.equals(rangeItem)) {
                            dataArray[k] = data.get(i);
                        }
                        i ++;
                    }
                    k ++;
                }

                tradeBarMoneyVo.setRange(rangeArray);
                tradeBarMoneyVo.setData(dataArray);

                tradeDetailVo.setTradeBarMoneyVo(tradeBarMoneyVo);
            }
        }

        return tradeDetailVo;
    }

    /**
     * 某个行业成立时间 + 1
     * @param tradeId
     * @param time
     */
    @Override
    public void addFoundTime(String tradeId, String time) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "成立时间");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndName(tradeChartEntity.getId(), time);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setName(time);
            tradeChartDetailEntity.setValue(1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + 1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delFoundTime(String tradeId, String time) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "成立时间");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndName(tradeChartEntity.getId(), time);
        if (tradeChartDetailEntity == null) {
            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - 1 >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - 1);
            }
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    @Override
    public void addFoundLocation(String tradeId, String location) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "成立地点");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndName(tradeChartEntity.getId(), location);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setName(location);
            tradeChartDetailEntity.setValue(1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + 1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delFoundLoaction(String tradeId, String location) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "成立地点");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndName(tradeChartEntity.getId(), location);
        if (tradeChartDetailEntity == null) {
            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - 1 >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - 1);
                tradeChartDetailEntity.setUpdateTime(new Date());
            }
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    @Override
    public void addFinacingRound(String tradeId, String round, int year) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资轮次");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndNameAndYear(tradeChartEntity.getId(), round, year);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setYear(year);
            tradeChartDetailEntity.setName(round);
            tradeChartDetailEntity.setValue(1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + 1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delFinacingRound(String tradeId, String round, int year) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资轮次");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndNameAndYear(tradeChartEntity.getId(), round, year);
        if (tradeChartDetailEntity == null) {
//            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - 1 >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - 1);
                tradeChartDetailEntity.setUpdateTime(new Date());
            }
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    @Override
    public void addValuations(String tradeId, int year, float valuation) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "总估值");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setYear(year);
            tradeChartDetailEntity.setValue(valuation);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + valuation);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delValuations(String tradeId, int year, float valuation) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "总估值");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - valuation >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - valuation);
                tradeChartDetailEntity.setUpdateTime(new Date());
            } else {
                tradeChartDetailEntity.setValue(0);
                tradeChartDetailEntity.setUpdateTime(new Date());
            }
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }


    @Override
    public void addFinacing(String tradeId, int year, float valuation) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资金额");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setYear(year);
            tradeChartDetailEntity.setValue(valuation);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + valuation);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delFinacing(String tradeId, int year, float valuation) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资金额");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - valuation >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - valuation);
            } else {
                tradeChartDetailEntity.setValue(0);
            }
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    @Override
    public void addFinacingThing(String tradeId, int year) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资事件");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            tradeChartDetailEntity = new TradeChartDetailEntity();
            tradeChartDetailEntity.setId(UUIDUtils.getUUID());
            tradeChartDetailEntity.setChartId(tradeChartEntity.getId());
            tradeChartDetailEntity.setYear(year);
            tradeChartDetailEntity.setValue(1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        } else {
            tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() + 1);
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }

    public void delFinacingThing(String tradeId, int year) {
        TradeChartEntity tradeChartEntity = tradeChartRepository.findByTradeIdAndName(tradeId, "投资事件");
        TradeChartDetailEntity tradeChartDetailEntity = tradeChartDetailRepository.findByChartIdAndYear(tradeChartEntity.getId(), year);
        if (tradeChartDetailEntity == null) {
            throw new NullPointerException("tradeChartDetailEntity is null");
        } else {
            if (tradeChartDetailEntity.getValue() - 1 >= 0) {
                tradeChartDetailEntity.setValue(tradeChartDetailEntity.getValue() - 1);
            }
            tradeChartDetailEntity.setUpdateTime(new Date());
            tradeChartDetailRepository.save(tradeChartDetailEntity);
        }
    }


}
